<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="../../templates/principal.xhtml">

    <ui:define name="content" id="content">
        <p:panel id="panel" header="Bem Vindo ao Cadastro de Endereços" style="">

            <p:dataTable id="tabEndereco" widgetVar="wtabEndereco" var="endereco" value="#{cepEnderecoView.cepEnderecoLazy}" filterEvent="enter" resizableColumns="true"
                         lazy="true" paginator="true" rows="10" paginatorPosition="top" emptyMessage="Nenhum Registro Retornado ..." style="text-align: left">

                <p:column headerText="C.E.P." sortBy="#{endereco.numCep}" filterBy="#{endereco.numCep}">
                    <h:outputText value="#{endereco.numCep}" />
                </p:column>

                <p:column headerText="Descrição" sortBy="#{endereco.dscLogradouro}" filterBy="#{endereco.dscLogradouro}">
                    <h:outputText value="#{endereco.dscLogradouro}" />
                </p:column>

                <p:column headerText="Bairro" sortBy="#{endereco.codBairro.dscBairro}" filterBy="#{endereco.codBairro.dscBairro}">
                    <h:outputText value="#{endereco.codBairro.dscBairro}" />
                </p:column>

                <p:column headerText="Cidade" sortBy="#{endereco.codBairro.codCidade.dscCidade}" filterBy="#{endereco.codCidade.dscCidade}">
                    <h:outputText value="#{endereco.codBairro.codCidade.dscCidade}" />
                </p:column>

                <p:column headerText="U.F." sortBy="#{endereco.codBairro.codCidade.codUf.codUf}" filterBy="#{endereco.codBairro.codCidade.codUf.codUf}">
                    <h:outputText value="#{endereco.codBairro.codCidade.codUf.codUf}" />
                </p:column>

                <p:column style="width: 150px;">
                    <h:panelGrid columns="4" styleClass="actions" cellpadding="0">
                        <p:commandButton id="selectButton" update=":frmPrincipal:panel, :frmPrincipal:messages" rendered="#{menuView.verificaAcesso(13,'consultar')}" 
                                         icon="ui-icon-search" title="Visualizar // Alterar Endereço" oncomplete="PF('enderecoDialog').show();">
                            <f:setPropertyActionListener value="#{endereco}" target="#{cepEnderecoView.selectedCepEndereco}" />
                            <f:setPropertyActionListener value="#{false}" target="#{menuView.salvar}" />
                        </p:commandButton>

                        <p:commandButton id="excluirButton" update=":frmPrincipal:panel, :frmPrincipal:messages" rendered="#{menuView.verificaAcesso(13,'excluir')}" 
                                         icon="ui-icon-trash" title="Excluir Endereço" oncomplete="PF('confirmation').show();">
                            <f:setPropertyActionListener value="#{endereco}" target="#{cepEnderecoView.selectedCepEndereco}" />
                            <f:setPropertyActionListener value="#{true}" target="#{menuView.salvar}" />
                        </p:commandButton>
                    </h:panelGrid>
                </p:column>

                <f:facet name="footer">
                    <div style="text-align: left;">
                        <p:commandButton id="incluirButton" actionListener="#{cepEnderecoView.preparaInserir}" rendered="#{menuView.verificaAcesso(13,'incluir')}"  
                                         update=":frmPrincipal:panel, :frmPrincipal:messages" value="Novo Endereço"
                                         icon="ui-icon-pencil" title="Cadastrar Novo Endereço" oncomplete="PF('enderecoDialog').show();" />
                    </div>
                </f:facet>
            </p:dataTable>

            <p:dialog id="dialogEndereco" header="Detalhes Endereço" widgetVar="enderecoDialog" showEffect="slide" hideEffect="slide" resizable="false">
                <p:focus context="displayEndereco" />
                <p:ajax event="close" update=":frmPrincipal:panel :frmPrincipal:messages" listener="#{menuView.fechaDialog}" />

                <h:panelGrid id="displayEndereco" columns="3" cellpadding="4">
                    <h:outputText value="C.E.P.:" />
                    <p:inputMask id="inCep" value="#{cepEnderecoView.selectedCepEndereco.numCep}" 
                                 style="width:100px; font-weight:bold;" mask="99999-999" />
                    <p:commandButton icon="ui-icon-search" title="Pesquisar C.E.P. pelo Correio" onclick="PF('pesquisaDialog').show();" type="button" />

                    <h:outputText value="Descrição:" />
                    <p:inputText id="inDescricao" value="#{cepEnderecoView.selectedCepEndereco.dscLogradouro}" style="width:900px; font-weight:bold; " />
                    <h:outputText value="" /> 

                    <h:outputText value="Bairro:" />
                    <p:inputText id="inBairro" value="#{cepEnderecoView.selectedCepEndereco.codBairro.dscBairro}" readonly="true" style="width:900px; font-weight:bold; " />
                    <p:commandButton icon="ui-icon-search" title="Pesquisar Bairro" onclick="PF('pesquisaBairroDialog').show();" type="button" />

                    <h:outputText value="Cidade:" />
                    <p:inputText id="inCidade" value="#{cepEnderecoView.selectedCepEndereco.codBairro.codCidade.dscCidade}" readonly="true" style="width:900px; font-weight:bold; " />
                    <h:outputText value="" />

                    <h:outputText value="U.F.:" />
                    <p:inputText id="inUf" value="#{cepEnderecoView.selectedCepEndereco.codBairro.codCidade.codUf.codUf}" readonly="true" style="width:50px; font-weight:bold; " />
                    <h:outputText value="" />

                    <f:facet name="footer">
                        <p:commandButton value="Inserir" update=":frmPrincipal:panel, :frmPrincipal:messages" rendered="#{menuView.verificaAcesso(13,'incluir')}"
                                         actionListener="#{cepEnderecoView.inserir}" oncomplete="PF('enderecoDialog').hide();" />
                        <p:commandButton value="Alterar" update=":frmPrincipal:panel, :frmPrincipal:messages" rendered="#{menuView.verificaAcesso(13,'alterar')}"
                                         actionListener="#{cepEnderecoView.alterar}" oncomplete="PF('enderecoDialog').hide();" />
                    </f:facet>
                </h:panelGrid>
            </p:dialog>

            <!-- PESQUISA BAIRRO -->
            <p:dialog id="dialogPesquisaBairro" header="Pesquisa dos Bairros" widgetVar="pesquisaBairroDialog" showEffect="slide" hideEffect="slide" 
                      width="800" resizable="false" position="right,bottom">

                <p:dataTable id="tabBairro" widgetVar="wtabBairro" var="bairro" value="#{cepEnderecoView.cepBairroLazy}" filterEvent="enter" 
                             paginator="true" rows="5" lazy="true" emptyMessage="Nenhum Bairro retornado ..." paginatorPosition="top">

                    <f:facet name="header">
                        Pesquise e selecione um Bairro
                    </f:facet>

                    <p:column headerText="Descrição" sortBy="#{bairro.dscBairro}" filterBy="#{bairro.dscBairro}" style="text-align: left; ">
                        <h:outputText value="#{bairro.dscBairro}" />
                    </p:column>

                    <p:column headerText="Cidade" sortBy="#{bairro.codCidade.dscCidade}" filterBy="#{bairro.codCidade.dscCidade}" style="text-align: left; ">
                        <h:outputText value="#{bairro.codCidade.dscCidade}" />
                    </p:column>

                    <p:column headerText="U.F." sortBy="#{bairro.codCidade.codUf.codUf}" filterBy="#{bairro.codCidade.codUf.codUf}" style="text-align: left; ">
                        <h:outputText value="#{bairro.codCidade.codUf.codUf}" />
                    </p:column>

                    <p:column style="width: 50px">
                        <p:panelGrid styleClass="actions" columns="1">
                            <p:commandButton id="selectButtonCidade" update=":frmPrincipal:displayEndereco, :frmPrincipal:messages" 
                                             icon="ui-icon-check" title="Selecionar Bairro" oncomplete="PF('pesquisaBairroDialog').hide();">
                                <f:setPropertyActionListener value="#{bairro}" target="#{cepEnderecoView.selectedBairro}" />
                            </p:commandButton>
                        </p:panelGrid>
                    </p:column>
                </p:dataTable>
            </p:dialog>

            <p:dialog header="Confirma a Exclusão desse Endereço: #{cepEnderecoView.selectedCepEndereco.dscLogradouro}?" widgetVar="confirmation">

                <p:commandButton value="Sim" update=":frmPrincipal:panel, :frmPrincipal:messages" oncomplete="PF('confirmation').hide();, PF('enderecoDialog').hide();"
                                 actionListener="#{cepEnderecoView.excluir}" />
                <p:commandButton value="Não" onclick="PF('confirmation').hide(), PF('enderecoDialog').hide()" type="button" />

            </p:dialog>
            <!--PESQUISAR ENDERECO NOS CORREIOS-->
            <p:dialog id="dialogPesquisa" header="Pesquisa de Endereços" widgetVar="pesquisaDialog"
                      showEffect="slide" hideEffect="slide" resizable="false">

                <p:panel id="panelPesquisa" style="text-align: left;">
                    <p:panelGrid id="displayPesquisa" columns="2" style="width: 100%;">
                        <h:outputText value="" />
                        <p:messages />

                        <h:outputText value="C.E.P.:" />
                        <p:inputText value="#{cepEnderecoView.numeroCep}" style="width:100px; font-weight:bold;" title="Digite o Numero do C.E.P. total ou parcial EX: 04029xxx" />
                        <h:outputText value="Descrição:" />
                        <p:inputText id="descLogradouro" value="#{cepEnderecoView.descLogradouro}" style="width:800px; font-weight:bold; " title="Digite Endereço total ou parcial EX: Ibirapuera" />

                        <f:facet name="footer">
                            <div style="text-align: center;">
                                <p:commandButton id="pesqLogradouro" icon="ui-icon-search" value="Pesquisar" actionListener="#{cepEnderecoView.PesquisarEndereco}"
                                                 update=":frmPrincipal:panelCep, :frmPrincipal:tabCep, :frmPrincipal:messages" />
                            </div>
                        </f:facet>

                    </p:panelGrid>

                    <p:panel id="panelCep">
                        <p:dataTable id="tabCep" widgetVar="wtabCep" var="cep" value="#{cepEnderecoView.lazyCeps}" filterEvent="enter"
                                     lazy="true" resizableColumns="true" paginator="true" rows="5" emptyMessage="Nenhum Endereço retornado ..." paginatorPosition="top">

                            <f:facet name="header">
                                Pesquise e selecione um Endereço
                            </f:facet>

                            <p:column headerText="C.E.P." sortBy="#{cep.numero}" filterBy="#{cep.numero}" style="text-align: left; ">
                                <h:outputText value="#{cep.numero}" title="Digite o Numero do C.E.P. total ou parcial EX: 99999xxx" />
                            </p:column>

                            <p:column headerText="Endereço" sortBy="#{cep.logradouro}" filterBy="#{cep.logradouro}" style="text-align: left; ">
                                <h:outputText value="#{cep.logradouro}" />
                            </p:column>

                            <p:column headerText="Bairro" sortBy="#{cep.bairro}" filterBy="#{cep.bairro}" style="text-align: left; ">
                                <h:outputText value="#{cep.bairro}" />
                            </p:column>

                            <p:column headerText="Cidade" sortBy="#{cep.localidade}" filterBy="#{cep.localidade}" style="text-align: left; ">
                                <h:outputText value="#{cep.localidade}" />
                            </p:column>

                            <p:column headerText="U.F." sortBy="#{cep.uf}" filterBy="#{cep.uf}" style="text-align: left; ">
                                <h:outputText value="#{cep.uf}" />
                            </p:column>

                            <p:column style="width: 50px">
                                <p:panelGrid styleClass="actions" columns="1">
                                    <p:commandButton id="selectButtonCep" update=":frmPrincipal:displayEndereco, :frmPrincipal:messages" 
                                                     action="#{cepEnderecoView.selecionaEndereco}" icon="ui-icon-check" title="Selecionar Endereço!" oncomplete="PF('pesquisaDialog').hide();">
                                        <f:setPropertyActionListener value="#{cep}" target="#{cepEnderecoView.cep}" />
                                    </p:commandButton>
                                </p:panelGrid>
                            </p:column>
                        </p:dataTable>
                    </p:panel>
                </p:panel>
            </p:dialog>
        </p:panel>
    </ui:define>
</ui:composition>